package com.ggh.jinjilive.ui.social.near.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemNearUserBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.social.near.NearUser;
import com.bumptech.glide.Glide;

import java.util.List;

public class NearUserAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<NearUser> nearUserList;


    public void Add(List<NearUser> nearUserList){
        this.nearUserList = nearUserList;
        notifyDataSetChanged();
    }

    public void setAdd(List<NearUser> nearUserList){
        if(null != this.nearUserList){
            this.nearUserList.addAll(nearUserList);
        }else {
            this.nearUserList = nearUserList;
        }
        notifyDataSetChanged();
    }



    private AddItemOnClickListener addItemOnClickListener;
    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener){
        this.addItemOnClickListener = addItemOnClickListener;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemNearUserBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_near_user, parent, false);
        return new ItemViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder recyclerViewholder, int position) {
        ItemViewHolder holder = (ItemViewHolder) recyclerViewholder;
        NearUser nearUser = nearUserList.get(position);
        if(null != nearUser){
            //头像
            Glide.with(holder.binding.imgPortrait).load(nearUser.getHead_portrait()).into(holder.binding.imgPortrait);
            //装扮
            Glide.with(holder.binding.imgDressup).load(nearUser.getHead_frame()).into(holder.binding.imgDressup);
            //VIP
            Glide.with(holder.binding.imgVip).load(nearUser.getB_img()).into(holder.binding.imgVip);
            if(nearUser.getSex() == 1){
                holder.binding.imgSex.setImageResource(R.mipmap.ic_male);
            }else {
                holder.binding.imgSex.setImageResource(R.mipmap.ic_female);
            }
            //
            if(TextUtils.isEmpty(nearUser.getIntroduce())){
                holder.binding.tvIntroduce.setText("这个人很懒什么都没有留下");
            }else {
                holder.binding.tvIntroduce.setText(nearUser.getIntroduce());
            }
            //
            holder.binding.tvNickname.setText(nearUser.getNickname());
            //
            holder.binding.tvLastTime.setText(nearUser.getLast_time());
            //
            holder.binding.tvDistance.setText(nearUser.getDistance() + "Km");

            holder.binding.llMessage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(position,nearUser);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return null == nearUserList ? 0 : nearUserList.size();
    }



    class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemNearUserBinding binding;

        public ItemViewHolder(@NonNull ItemNearUserBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}
